﻿package bipede.membres
{
	import flash.geom.Point;
	import fl.ik.*;	
	import fab.geom.*;
	import physic.Obstacle;
	import events.MoveEvent;
	
	public class Bras
	{
		public var squelette:Squelette;
		public var ikBoneMain:IKBone;
		public var ikBoneAvantbras:IKBone;
		public var ikBoneBras:IKBone;
		public var ikMoverPoignet:IKMover;
		public var ikMoverDoigt:IKMover;
		public var ikMoverCoude:IKMover;
		
		private var fixed:DoublePoint;
		
		//public var position:Point = new Point(0, 0);
		//public var physic_contrainte:Point;
		
		private var _longueur:int = 0;
		private var _longueur_totale:int = 0;
		
		public function Bras( _squelette:Squelette, _ikBone:IKBone )
		{
			squelette = _squelette;
			
			ikBoneMain = _ikBone;
			ikBoneAvantbras = _ikBone.headJoint.bone;
			ikBoneBras = ikBoneMain.headJoint.bone.headJoint.bone;
			
			ikMoverCoude = new IKMover( ikBoneMain.headJoint.parent, coude );
			ikMoverPoignet = new IKMover( ikBoneMain.headJoint, poignet );
			ikMoverDoigt = new IKMover( ikBoneMain.tailJoint, doigt );
			
			//trace(ikBone.headJoint.parent.name);
			//trace(ikBone.headJoint.parent.parent.name);
			
			ikMoverPoignet.limitByIteration = true;
			ikMoverPoignet.iterationLimit = 4;
			ikMoverDoigt.limitByIteration = true;
			ikMoverDoigt.iterationLimit = 4;	
			ikMoverCoude.limitByIteration = true;
			ikMoverCoude.iterationLimit = 4;			
		}

		public function get doigt():Point
		{
			return ikBoneMain.tailJoint.position;
		}
		public function get poignet():Point
		{
			return ikBoneMain.headJoint.position;
		}		
		public function get coude():Point
		{
			return ikBoneMain.headJoint.parent.position;
		}
		public function get longueur():int
		{
			if ( _longueur == 0 ) _longueur = Point.distance( ikBoneMain.headJoint.position, ikBoneMain.tailJoint.position );
			return _longueur;
		}		
		public function get longueur_totale():int
		{
			if ( _longueur_totale == 0 )
			{
				_longueur_totale = Point.distance( ikBoneMain.headJoint.position, ikBoneMain.tailJoint.position );
				_longueur_totale+= Point.distance( ikBoneAvantbras.headJoint.position, ikBoneAvantbras.tailJoint.position );
				_longueur_totale+= Point.distance( ikBoneBras.headJoint.position, ikBoneBras.tailJoint.position );
			}
			return _longueur_totale;
		}
		public function get portee():Circle
		{
			return new Circle( ikBoneBras.headJoint.position, longueur_totale );
		}		
		public function deplace( event:MoveEvent ):void
		{
			//trace(this + "move " + event );
			fixed = null;
			move( (event.point as DoublePoint).position1, (event.point as DoublePoint).position2 );
		}		
		public function move( _talon:Point, _orteil:Point ):void
		{
			ikMoverDoigt.moveTo( _orteil );
			ikMoverPoignet.moveTo( _talon );
		}
		public function deplace_extremite( event:MoveEvent ):void
		{
			fixed = null;
			move_extremite( event.point );
		}		
		public function move_extremite( p:Point ):void
		{
			ikMoverDoigt.moveTo( p );
		}		
		public function toString():String
		{
			return "Bras : " + ikBoneMain.name;
		}	
	}	
}